package com.ytktt.study.week9.task3.rpcfx;

import com.alibaba.fastjson.parser.ParserConfig;
import com.ytktt.study.week9.task3.rpcfx.client.Rpcfx;
import com.ytktt.study.week9.task3.rpcfx.constant.RpcfxConstant;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

/**
 * 消费者应用启动器
 *
 * @author ytk
 */
@Slf4j
@SpringBootApplication
@RestController
public class RpcfxClientApplication {

    @Autowired
    private Rpcfx rpcfx;

    // 二方库
    // 三方库 lib
    // nexus, userserivce -> userdao -> user
    //

    public static void main(String[] args) {

        ParserConfig.getGlobalInstance().addAccept(RpcfxConstant.SERVICE_PACKAGE_PREFIX + '.' + RpcfxConstant.API_PACKAGE_PREFIX + '.');

        // UserService service = new xxx();
        // service.findById

//        UserService userService = Rpcfx.create("userService", "http://127.0.0.1:8888/", "g2", "2.0");
//        User user = userService.findById(1);
//        System.out.println("find user id=1 from server: " + user.getName());

//		OrderService orderService = Rpcfx.create(OrderService.class, "http://localhost:8080/");
//		Order order = orderService.findOrderById(1992129);
//		System.out.println(String.format("find order name=%s, amount=%f",order.getName(),order.getAmount()));
//

		SpringApplication.run(RpcfxClientApplication.class, args);
    }

    @GetMapping("/{serviceName}")
    public String test(@PathVariable String serviceName) throws Exception {
        UserService userService = this.rpcfx.create(serviceName);
        User user = userService.findById(1);
        System.out.println("find user id=1 from server: " + user.getName());
        return user.getName();
    }
}
